from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class HomePage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.search_box_locator = (By.ID, "search-box")
        self.search_button_locator = (By.ID, "search-button")
        self.category_dropdown_locator = (By.ID, "category-dropdown")
        self.category_option_locator = (By.XPATH, "//select[@id='category-dropdown']/option")
        self.featured_products_locator = (By.CLASS_NAME, "featured-product")
        self.banner_ad_locator = (By.ID, "banner-ad")
        self.login_link_locator = (By.LINK_TEXT, "登录")
        self.signup_link_locator = (By.LINK_TEXT, "注册")
        self.user_profile_icon_locator = (By.ID, "user-profile-icon")
        self.notification_icon_locator = (By.ID, "notification-icon")

    def navigate(self):
        self.driver.get('http://example.com/home')

    def search_for_product(self, query):
        search_box = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.search_box_locator)
        )
        search_box.send_keys(query)
        search_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.search_button_locator)
        )
        search_button.click()

    def select_category(self, category):
        category_dropdown = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.category_dropdown_locator)
        )
        category_dropdown.click()
        options = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.category_option_locator)
        )
        for option in options:
            if option.text == category:
                option.click()
                break

    def verify_featured_products(self, expected_product_names):
        featured_products = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.featured_products_locator)
        )
        actual_product_names = [product.text for product in featured_products]
        return all(name in actual_product_names for name in expected_product_names)

    def click_login_link(self):
        login_link = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.login_link_locator)
        )
        login_link.click()

    def click_signup_link(self):
        signup_link = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.signup_link_locator)
        )
        signup_link.click()

    def click_user_profile_icon(self):
        user_profile_icon = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.user_profile_icon_locator)
        )
        user_profile_icon.click()

    def click_notification_icon(self):
        notification_icon = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.notification_icon_locator)
        )
        notification_icon.click()

    def verify_banner_ad(self, expected_text):
        banner_ad = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.banner_ad_locator)
        )
        return expected_text in banner_ad.text